import 'package:get/get.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/server/api/live_api.dart';
import 'package:ball_lion/server/http/request_header.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/utils/utils.dart';

class ResourceService extends GetxService {
  //专家客服
  ResourceEntity? expertCS;
  //启动页配置
  List<ResourceEntity>? _appQsLaunchPage;
  //导航栏主题
  List<ResourceEntity>? navigationBarTheme;
  //app开机资源
  List<ResourceEntity>? appStart;
  //app轮播图
  List<ResourceEntity> lbt = [];
  //导航栏初始index
  ResourceEntity? bottomInitialIndex;
  //首页默认tab
  ResourceEntity? homeDefaultTab;
  //邀请好友提示语
  ResourceEntity? inviteTips;
  //提示资源
  List<ResourceEntity> tips = [];
  //邀请好友配置
  ResourceEntity? inviteConfig;
  //专家申请顶部引导
  ResourceEntity? expertApplyGuide;
  //进入h5
  ResourceEntity? starth5;
  //app弹窗资源
  List<ResourceEntity> dialog = [];
  //登录后弹窗资源
  List<ResourceEntity> loginDialog = [];
  //app浮窗广告
  List<ResourceEntity> floatAds = [];
  //app页面顶部图
  List<ResourceEntity> banner = [];
  //客服信息
  List<ResourceEntity>? contact;

  bool hasLive = false;

  Future<ResourceService> init() async {
    // getAppLaunch();
    return this;
  }

  Future getResource() async {
    getAppListConfig();
    getLive();
  }

  Future getLive() async {
    var list = await LiveApi.getLiveList(0);
    hasLive = list.where((element) => element.status == 1).toList().hasValue;
  }

  //启动页
  Future getAppLaunchConfig() async {
    // _appLaunch = SpUtils.appLaunch?.map((e) => ResourceEntity.fromJson(e)).toList();
    try {
      final reslut =
          await OtherApi.getAppList("app_launch", receiveTimeout: 2000);
      if (reslut != null) {
        _appQsLaunchPage = reslut;
        SpUtils.appLaunch = reslut.map((e) => e.toJson()).toList();
      }
    } catch (e) {}
  }

  //启动配置
  Future getAppStartConfig() async {
    // _appLaunch = SpUtils.appLaunch?.map((e) => ResourceEntity.fromJson(e)).toList();
    // log('${HeaderDeviceInfo.descrption}');
    try {
      RequestHeader.print();
      final reslut =
          await OtherApi.getAppList("app_start", receiveTimeout: 2000);
      if (reslut != null) {
        appStart = reslut;
        for (ResourceEntity entity in reslut) {
          if (entity.button == 'news_tab_default') {
            homeDefaultTab = entity;
          }
          if (entity.title == 'initial_navigation_index') {
            bottomInitialIndex = entity;
          }
          if (entity.button == 'invite_info_qds') {
            inviteConfig = entity;
          }
          if (entity.title == 'expert_apply_banner') {
            expertApplyGuide = entity;
          }
          if (entity.button == 'start_h5') {
            starth5 = entity;
          }
          if (entity.button == 'dialog') {
            dialog.add(entity);
          }
          if (entity.button == 'dialog_video') {
            dialog.add(entity);
          }
        }
      }
    } catch (e) {}
  }

  //获取登录后配置
  Future getAppLoginConfig() async {
    try {
      final reslut =
          await OtherApi.getAppList("app_login", receiveTimeout: 2000);
      if (reslut != null) {
        bool hasInviteTip = false;
        for (ResourceEntity entity in reslut) {
          if (entity.title == 'invite_tip') {
            inviteTips = entity;
            hasInviteTip = true;
          }
          if (entity.button == 'dialog') {
            loginDialog.add(entity);
          }
        }
        if (hasInviteTip == false) {
          inviteTips = null;
        }
      }
    } catch (e) {}
  }

  logout() {
    inviteTips = null;
    loginDialog = [];
  }

  ResourceEntity? get appQsLaunchPage {
    if (_appQsLaunchPage == null || _appQsLaunchPage?.length == 0) {
      return null;
    } else {
      return _appQsLaunchPage?.first;
    }
  }

  List<ResourceEntity>? get webgames =>
      appStart?.filterOrNull((item) => item.title == "web-game-entry");

  void getAppListConfig() async {
    final results = await OtherApi.getAppList(
        "navigation_theme,contact_app_qs,app_lbt,app_res_tips,app_ads_float",
        receiveTimeout: 2000);
    //导航栏主题
    List<ResourceEntity>? navigationThemeResult = results?.filter((f) {
      return f.classId == "navigation_theme";
    });
    if (navigationThemeResult != null) {
      navigationBarTheme = navigationThemeResult;
    }
    //专家客服
    expertCS = ResourceEntity.fromJson(SpUtils.expertContact ?? {});
    List<ResourceEntity>? appResContactResult = results?.filter((f) {
      return f.classId == "contact_app_qs";
    });
    if (appResContactResult != null) {
      contact = appResContactResult; //客服
      for (ResourceEntity entity in appResContactResult) {
        if (entity.button == 'expert_cs') {
          expertCS = entity;
        }
      }
      SpUtils.expertContact = expertCS!.toJson();
    }
    //轮播图
    List<ResourceEntity>? appLbtResult = results?.filter((f) {
      return f.classId == "app_lbt";
    });
    if (appLbtResult != null) {
      lbt = appLbtResult;
    }
    //提示资源
    List<ResourceEntity>? appResTipsResult = results?.filter((f) {
      return f.classId == "app_res_tips";
    });
    if (appResTipsResult != null) {
      tips = appResTipsResult;
    }
    //浮窗广告
    List<ResourceEntity>? appAdsFloatResult = results?.filter((f) {
      return f.classId == "app_ads_float";
    });
    if (appAdsFloatResult != null) {
      floatAds = appAdsFloatResult;
    }
  }
}
